﻿using System;
using System.Collections.Generic;
using System.ComponentModel.DataAnnotations;
using System.Data;
using System.IO;
using System.Linq;
using System.Reflection;
using System.Text;
using System.Threading;
using System.Threading.Tasks;

namespace Daq
{
    public static class EnumHelper
	{
		/// <summary>
		/// 获得枚举的displayName
		/// </summary>
		/// <param name="eum"></param>
		/// <returns></returns>
		public static string GetDisplayName(this Enum eum)
		{
            if (eum == null) return "";
			var type = eum.GetType();//先获取这个枚举的类型
			var field = type.GetField(eum.ToString());//通过这个类型获取到值
            if (field == null) return "";
            if(field.GetCustomAttribute(typeof(DisplayAttribute)) is DisplayAttribute obj)
            {
                return obj.Name ?? "";
            }
            return "";
        }
    }
	public class EnumHelper<T>
	{
		public static List<T> ToList()
		{
			return Enum.GetValues(typeof(T)).Cast<T>().ToList();
		}
	}

	public class FileHelper
	{
        public static async Task WriteTextAsync(string filePath, string text)
        {
            // 获取文件所在目录路径
            string directoryPath = Path.GetDirectoryName(filePath);

            // 如果目录不存在，则创建目录
            if (!Directory.Exists(directoryPath))
            {
                Directory.CreateDirectory(directoryPath);
            }

            byte[] encodedText = Encoding.UTF8.GetBytes(text);

            using (FileStream sourceStream = new FileStream(filePath, FileMode.Create, FileAccess.Write, FileShare.None, bufferSize: 4096, useAsync: true))
            {
                await sourceStream.WriteAsync(encodedText, 0, encodedText.Length);
            }
        }
    }
}
